package mop.gui.task;

import java.util.LinkedList;
import mop.controller.CreateTaskController;
import mop.gui.GUI;
import mop.model.project.ProjectInterface;
import mop.model.task.TaskInterface;

/**
 * AssignTaskDialog
 * 
 * @author  Laurens
 */
public class AssignTaskDialog extends javax.swing.JDialog
{
    /**
     * Constructor.
     * 
     * @param   gui
     * @param   assignTaskToProjectController
     * @param   task
     */
    public AssignTaskDialog(GUI gui, CreateTaskController createTaskController, TaskInterface task)
    {
        super(gui.getMainWindow(), true);
        initComponents();
        
        setGui(gui);
        this.createTaskController = createTaskController;

        // Set task
        this.task = task;
        this.txtId.setText(task.getId());
        
        // Load projects and select current assignment.
        loadProjects();
        if (task.getProject() == null)
            this.cmbProjects.setSelectedItem(" ");
        else
            this.cmbProjects.setSelectedItem(task.getProject().getDescription());
    }
    
    /***************************************************************************
     *                          GUI reference
     ***************************************************************************/
    
    private GUI gui;
    
    /**
     * Sets the GUI.
     * 
     * @param   gui
     * @post    gui is set.
     */
    private void setGui(GUI gui)
    {
        this.gui = gui;
    }
    
    /**
     * Returns the GUI reference.
     * 
     * @return  this.gui
     */
    public GUI getGui()
    {
        return this.gui;
    }
    
    /***************************************************************************
     *                          Controllers
     ***************************************************************************/
    
    private CreateTaskController createTaskController;
    
    /***************************************************************************
     *                          Selected task
     ***************************************************************************/
    
    private TaskInterface task;
    
    /***************************************************************************
     *                          Dialog-specific functionality
     ***************************************************************************/

    private LinkedList<ProjectInterface> projects = new LinkedList<ProjectInterface>();
    
    /**
     * Loads projects combobox.
     */
    private void loadProjects()
    {
        if (task.getProject() == null) this.cmbProjects.addItem(" ");
        projects = (LinkedList<ProjectInterface>) createTaskController.getProjectList();
        for(ProjectInterface p : projects)
            this.cmbProjects.addItem(p.getDescription());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblAssignTask = new javax.swing.JLabel();
        lblTask = new javax.swing.JLabel();
        btnAssign = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        lblProject = new javax.swing.JLabel();
        cmbProjects = new javax.swing.JComboBox();
        txtId = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Assign Task");

        lblAssignTask.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        lblAssignTask.setText("Assign Task to a Project?");
        lblAssignTask.setName("lblAssignTask"); // NOI18N

        lblTask.setText("Task Id");
        lblTask.setName("lblTask"); // NOI18N

        btnAssign.setText("Assign");
        btnAssign.setName("btnAssign"); // NOI18N
        btnAssign.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAssignActionPerformed(evt);
            }
        });

        btnCancel.setText("Don't Assign");
        btnCancel.setName("btnCancel"); // NOI18N
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        lblProject.setText("Choose a project");
        lblProject.setName("lblProject"); // NOI18N

        cmbProjects.setName("cmbProjects"); // NOI18N

        txtId.setEditable(false);
        txtId.setName("txtId"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblAssignTask)
                    .addComponent(lblTask)
                    .addComponent(txtId, javax.swing.GroupLayout.DEFAULT_SIZE, 327, Short.MAX_VALUE)
                    .addComponent(lblProject)
                    .addComponent(cmbProjects, 0, 327, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnAssign, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblAssignTask)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblTask)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblProject)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cmbProjects, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAssign)
                    .addComponent(btnCancel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

/**
 * Closes this dialog.
 * 
 * @param   evt
 */
private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
    this.dispose();
}//GEN-LAST:event_btnCancelActionPerformed

private void btnAssignActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAssignActionPerformed
    try
    {
        if ( this.cmbProjects.getSelectedIndex() > 0 ) {
            String project = projects.get(this.cmbProjects.getSelectedIndex() - 1).getId();
            createTaskController.assignTaskToProject(this.task.getId(), project);
            gui.getMainWindow().update();
            gui.handleSuccess("Task assigned to project!");
        }
        this.dispose();
    }
    catch (Throwable exception)
    {
        gui.handleError(exception);
    }
}//GEN-LAST:event_btnAssignActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAssign;
    private javax.swing.JButton btnCancel;
    private javax.swing.JComboBox cmbProjects;
    private javax.swing.JLabel lblAssignTask;
    private javax.swing.JLabel lblProject;
    private javax.swing.JLabel lblTask;
    private javax.swing.JTextField txtId;
    // End of variables declaration//GEN-END:variables
}